package com.etc.RCCPA002.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.etc.RCCPA002.service.IAdminBannerService;
import com.etc.RCCPA002.vo.BannerVo;
import com.etc.common.Code;
import com.etc.common.CodeMessage;
import com.etc.common.CommonUtil;
import com.etc.common.ServiceException;
import com.etc.common.SessionCode;
import com.etc.common.UtilException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 
 * Filename:    AdminBannerController.java  
 * Description:   Banner控制器
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     mazheng 
 * @version:    1.0  
 * Create at:   2020年3月3日 下午8:29:54  
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2020年3月3日      mazheng      1.0         1.0 Version  
 *
 */
@RestController
@SuppressWarnings("rawtypes")
public class AdminBannerController {

    @Autowired
    IAdminBannerService service;
    /**
     * 获取所有模块
     * @return
     */
    @GetMapping("admin/getAllModel.action")
    public CodeMessage getAllModel() {
        return CodeMessage.success(Code.SUCESS, null, service.findAllModel());
    }
    
    /**
     * 根据检索条件，获取banner列表
     * @param vo
     * @return
     */
    @RequestMapping("admin/banner/list")
    public String getBannerList(BannerVo vo) {
        
        int page = vo.getPage();
        int limit = vo.getLimit();
                
        
        PageHelper.startPage(page, limit);
        List<Map> mlist = service.findAllBannerList(vo);

        PageInfo<Map> p = new PageInfo<>(mlist);
        if (mlist == null || mlist.size() == 0) {
            return CodeMessage.success(Code.SUCCESS_SHOW, "没有数据", p, true);
        }
        return CodeMessage.success(Code.SUCESS, "检索成功", p, true);
    }
    
    /**
     * 根据ID，修改状态
     * @param id
     * @param statflg
     * @return
     */
    @RequestMapping("admin/banner/{id}/updatestuats.action")
    public CodeMessage updatestuats(@PathVariable Integer id,Integer statflg) {
        
      
        Map map = null;
        try {
            map = service.updatestatus(id, statflg);
        } catch (ServiceException e) {
            e.printStackTrace();
            return CodeMessage.faild(Code.ERROR, e.getMessage());
        }
       
        return CodeMessage.success(Code.SUCCESS_SHOW, "修改成功", map);
    }
    
    /**
     * 根据条件更新数据
     * @param data
     * @return
     */
    @RequestMapping("admin/banner/updateBanner.action")
    public CodeMessage updatestuats(@RequestBody HashMap data) {
        
        try {
            if (data.get("imgpath2") != null) {
                data.put("imgpath3", CommonUtil.getByteArrya(data.get("imgpath2")));
            }
            if (data.get("mobimgpath2") != null) {
                data.put("mobimgpath3", CommonUtil.getByteArrya(data.get("mobimgpath3")));
            }
            Map map = service.updatesById(data);
            return CodeMessage.success(Code.SUCCESS_SHOW, "修改成功", map);
        } catch (UtilException | ServiceException e) {
            e.printStackTrace();
            return CodeMessage.success(Code.FAIL, e.getMessage(), null);
        }
    }
    
    /**
     * 根据ID删除一条记录
     * @param id
     * @return
     */
    @GetMapping("/admin/banner/{id}/deleteSingleBanner.action")
    public CodeMessage deleteByid(@PathVariable Integer id) {
        
        if (id == null) {
            return CodeMessage.faild(Code.FAIL, "删除失败");
        }
        
        try {
            service.deleteById(id);
        } catch (ServiceException e) {
            e.printStackTrace();
            return CodeMessage.faild(Code.ERROR, e.getMessage());
        }
        return CodeMessage.success(Code.SUCESS, "删除成功", null);
    }
    /**
     * 根据ids删除 对应记录
     * @param ids
     * @return
     */
    @PostMapping("/admin/banner/deleteAll.action")
    public CodeMessage deleteByid(@RequestBody int[] ids) {
        
        if (ids == null) {
            return CodeMessage.faild(Code.FAIL, "删除失败");
        }
        
        try {
            service.deleteByIds(ids);
        } catch (ServiceException e) {
            e.printStackTrace();
            return CodeMessage.faild(Code.FAIL, e.getMessage());
        }
        return CodeMessage.success(Code.SUCCESS_SHOW, ids.length + "条记录删除成功", null);
    }
    
    /**
     * 创建banner
     * @param data
     * @param req
     * @return
     */
    @RequestMapping("admin/banner/createBanner.action")
    public CodeMessage createBanner(@RequestBody HashMap data, HttpServletRequest req) {
        System.out.println( req.getSession().getId());
        System.out.println( req.getSession().getAttribute(SessionCode.ADMINUSER));
        data.put("username", req.getSession().getAttribute(SessionCode.ADMINUSER));
        try {
            if (data.get("imgpath2") != null) {
                data.put("imgpath3", CommonUtil.getByteArrya(data.get("imgpath2")));
            }
            if (data.get("mobimgpath2") != null) {
                data.put("mobimgpath3", CommonUtil.getByteArrya(data.get("mobimgpath3")));
            }
            service.createBanner(data);
            return CodeMessage.success(Code.SUCCESS_SHOW, data.get("title") + " 添加成功", null);
        } catch (UtilException | ServiceException e) {
            e.printStackTrace();
            return CodeMessage.success(Code.FAIL, e.getMessage(), null);
        }
    }
    
}
